package com.situ.mall.manager.intercepter;

import com.situ.mall.manager.entity.Admin;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component  //将类放进spring的bean容器中
public class AdminIntercepter implements HandlerInterceptor {
    //继承处理器拦截器

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
       //执行拦截器之前执行
       //验证登录用户
        HttpSession session = request.getSession();
        Admin admin = (Admin) session.getAttribute("admin");
        if(admin == null){
            //没登陆
            //跳转页面登录 但是会套娃
            response.sendRedirect("/admin/login.html");
        }
        return true;//放行 如果拦截返回false

    }

    @Override
    public void postHandle(HttpServletRequest request,
                           HttpServletResponse response,
                           Object handler, ModelAndView modelAndView) throws Exception {
        //处理器处理完成时执行



    }

    @Override
    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response,
                                Object handler, Exception ex) throws Exception {


    }
}
